/*
 * Copyright (c) 2013-2015 Charkey. All rights reserved.
 *
 * This software is the confidential and proprietary information of Charkey.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the agreements you entered into with Charkey.
 *
 * Charkey MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 * Charkey SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package cn.simastudio.talos.inter.controller.web;

import cn.simastudio.talos.inter.model.inst.DemoCountry;
import cn.simastudio.talos.inter.service.inst.DemoService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Usage: DemoController
 *
 * @author Charkey
 * @date 2015/4/28 14:51
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Controller
@RequestMapping("/test")
public class DemoController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    private DemoService demoService;

    @Autowired
    private CacheManager cacheManager;

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete() {
        try {
            demoService.deleteByPrimaryKey(183);
        } catch (Exception e) {
            LOGGER.debug("error occurred");
        }
        return "SUCCESS";
    }

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public String getName() {
        demoService.getUser1(1);
        demoService.getUser3(1);
        //throw new UserBlockedAuthException("Just blocked");
        return "/jsp/test.jsp";
    }

    @ResponseBody
    @RequestMapping(value = "/country", method = RequestMethod.GET)
    public Object getCountry() {
        return demoService.selectByPrimaryKey(35);
    }

    @RequestMapping(value = "/updateCountry", method = RequestMethod.POST)
    public String updateCountry(@Param("id") int id,
                                @Param("countryName") String countryName,
                                @Param("countryCode") String countryCode) {
        DemoCountry demoCountry = new DemoCountry(id, countryName, countryCode);
        demoService.updateByPrimaryKey(demoCountry);
        return "/jsp/error/test.jsp";
    }

    @RequestMapping("/put_cache")
    public void testCache1() {
        cacheManager.getCache("access_token_cache").put("name", "charkey");
    }

    @RequestMapping("/get_cache")
    public void getCache1() {
        cacheManager.getCache("access_token_cache").get("name").get();
    }
}
